package cn.ffcs.uec.organization.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Repository;

import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.organization.dao.UecOrgDeptDao;
import cn.ffcs.uec.organization.model.UecOrgDept;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.organization.vo.OrganizationRelationVo;
import cn.ffcs.uec.organization.vo.UecOrgDeptVo;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecOrgDeptDao")
public class UecOrgDeptDaoImpl extends BaseDaoImpl implements UecOrgDeptDao {

    @Override
    public UecOrgDept findUecOrgDeptByOrganizationRelationVo(OrganizationRelationVo organizationRelationVo) {
        if (null != organizationRelationVo) {
            StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_ORG_DEPT T WHERE T.STATUS_CD = ? ");
            List<Object> params = new ArrayList<Object>();
            params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
            if (null != organizationRelationVo.getOrg()) {
                sb.append(" AND T.PK_DEPT = ? ");
                params.add(organizationRelationVo.getOrg());
            }
            UecOrgDept uecOrgDept = super.jdbcFindObject(sb.toString(), params, UecOrgDept.class);
            return uecOrgDept;
        }
        return null;
    }

    @Override
    public UecOrgDept findUecOrgOrgsByUecOrgDept(UecOrgDept uecOrgDept) {
        if (null != uecOrgDept) {
            StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_ORG_DEPT T WHERE T.STATUS_CD = ? ");
            List<Object> params = new ArrayList<Object>();
            params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
            if (null != uecOrgDept.getPkOrg()) {
                sb.append(" AND T.PK_DEPT = ? ");
                params.add(uecOrgDept.getPkOrg());
            }
            List<UecOrgDept> list = super.jdbcFindList(sb.toString(), params, UecOrgDept.class);
            if (list.size() > 0 && list != null) {
                return list.get(0);
            }
        }
        return null;
    }

    @Override
    public UecOrgDept findUecOrgDeptByOrgDept(UecOrgDept uecOrgDept) {
        if (null != uecOrgDept) {
            StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_ORG_DEPT T WHERE T.STATUS_CD = ? ");
            List<Object> params = new ArrayList<Object>();
            params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
            if (null != uecOrgDept.getPkDept()) {
                sb.append(" AND T.PK_DEPT = ? ");
                params.add(uecOrgDept.getPkDept());
            }
            List<UecOrgDept> list = super.jdbcFindList(sb.toString(), params, UecOrgDept.class);
            if (list.size() > 0 && list != null) {
                return list.get(0);
            }
        }
        return null;
    }

    @SuppressWarnings("unused")
    @Override
    public PageInfo queryUecOrgDeptPage(UecOrgDeptVo uecOrgDeptVo, int currentPage, int pageSize) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * FROM (SELECT M.* FROM (SELECT T.PK_ORG ORG,T.CODE,"
                + "T.PK_FATHERORG PORG,T.NAME ORGNAME,'' SHORTNAME,'' GLBDEF1,"
                + "'' PRINCIPAL,'ORG' ORGTYPE FROM UEC_ORG_ORGS T UNION "
                + " SELECT T1.PK_DEPT,T1.CODE,NVL(REPLACE(T1.PK_FATHERORG, '~', ''), T1.PK_ORG),"
                + " T1.NAME,T1.SHORTNAME,T1.GLBDEF1,T1.PRINCIPAL,'DEPT' "
                + " FROM UEC_ORG_DEPT T1 WHERE T1.hrcanceled = 'N' "
                + " AND T1.DEPTCANCELDATE IS NULL) M START WITH M.PORG = ? "
                + " CONNECT BY PRIOR M.ORG = M.PORG) u WHERE u.ORGTYPE = 'DEPT'");
        List<Object> params = new ArrayList<Object>();
        params.add(uecOrgDeptVo.getPorg());
        if (null != uecOrgDeptVo) {
            if (!StrUtil.isNullOrEmpty(uecOrgDeptVo.getOrg())) {
                sql.append(" AND u.org = ? ");
                params.add(uecOrgDeptVo.getOrg());
            }
            if (!StrUtil.isNullOrEmpty(uecOrgDeptVo.getCode())) {
                sql.append(" AND u.code = ? ");
                params.add(uecOrgDeptVo.getCode());
            }
            if (!StrUtil.isNullOrEmpty(uecOrgDeptVo.getOrgname())) {
                sql.append(" AND u.orgname like ?");
                params.add("%" + StringEscapeUtils.escapeSql(uecOrgDeptVo.getOrgname()) + "%");
            }
            return super.jdbcFindPageInfo(sql.toString(), params, currentPage, pageSize, UecOrgDeptVo.class);
        }
        return null;
    }

    @Override
    public PageInfo queryUecOrgDeptPageInfo(UecOrgDeptVo uecOrgDeptVo, int currentPage, int pageSize) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT u.PK_DEPT ORG,u.CODE," + "NVL(REPLACE(u.PK_FATHERORG, '~', ''), u.PK_ORG) PORG,"
                + "u.NAME ORGNAME," + "'DEPT' ORGTYPE FROM UEC_ORG_DEPT u where 1=1 ");
        List<Object> params = new ArrayList<Object>();
        if (null != uecOrgDeptVo) {
            if (!StrUtil.isNullOrEmpty(uecOrgDeptVo.getOrg())) {
                sql.append(" AND u.org = ? ");
                params.add(uecOrgDeptVo.getOrg());
            }
            if (!StrUtil.isNullOrEmpty(uecOrgDeptVo.getCode())) {
                sql.append(" AND u.code = ? ");
                params.add(uecOrgDeptVo.getCode());
            }
            if (!StrUtil.isNullOrEmpty(uecOrgDeptVo.getOrgname())) {
                sql.append(" AND u.orgname like ?");
                params.add("%" + StringEscapeUtils.escapeSql(uecOrgDeptVo.getOrgname()) + "%");
            }
            return super.jdbcFindPageInfo(sql.toString(), params, currentPage, pageSize, UecOrgDeptVo.class);
        }
        return null;
    }

    @Override
    public PageInfo findUecOrgDeptByOrgOrgs(UecOrgOrgs uecOrgOrgs, int currentPage, int pageSize) {
        if (null != uecOrgOrgs) {
            StringBuilder sb = new StringBuilder(
                    " SELECT T1.PK_DEPT ORG,T1.CODE CODE,NVL(REPLACE(T1.PK_FATHERORG, '~', ''), T1.PK_ORG) PORG,"
                            + "T1.NAME ORGNAME,T1.SHORTNAME,T1.GLBDEF1,T1.PRINCIPAL,'DEPT' ORGTYPE FROM UEC_ORG_DEPT T1 "
                            + "WHERE T1.hrcanceled = 'N' AND T1.DEPTCANCELDATE IS NULL AND T1.STATUS_CD = ? ");
            List<Object> params = new ArrayList<Object>();
            params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
            if (!StrUtil.isNullOrEmpty(uecOrgOrgs.getPkOrg())) {
                sb.append(" AND T1.PK_ORG = ? ");
                params.add(uecOrgOrgs.getPkOrg());
            }
            return super.jdbcFindPageInfo(sb.toString(), params, currentPage, pageSize, UecOrgDeptVo.class);
        }
        return null;
    }

    @Override
    public String getUecOrgDeptNameByPkDept(String pkDept) {
        if (!StrUtil.isEmpty(pkDept)) {
            StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_ORG_DEPT T WHERE T.STATUS_CD = ? ");
            List<Object> params = new ArrayList<Object>();
            params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
            sb.append(" AND T.PK_DEPT = ? ");
            params.add(pkDept);
            List<UecOrgDept> list = super.jdbcFindList(sb.toString(), params, UecOrgDept.class);
            if (list.size() > 0 && list != null) {
                return list.get(0).getName();
            }
        }
        return "";
    }

    @Override
    public PageInfo queryAllColumnUecOrgDeptPage(UecOrgDept uecOrgDept, int currentPage, int pageSize) {
        String orgName = PermissionUtil.getManagerOrg();
        StringBuilder sb = new StringBuilder(
                " SELECT T1.* FROM UEC_ORG_DEPT T1 LEFT JOIN UEC_ORG_ORGS T2 ON (T1.PK_ORG = T2.PK_ORG) WHERE T1.ENABLESTATE = '2' "
                        + " AND T1.HRCANCELED = 'N' AND T1.DEPTCANCELDATE IS NULL AND T1.STATUS_CD = ? ");
        List<Object> params = new ArrayList<Object>();
        params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
        if (null != uecOrgDept) {
            if (uecOrgDept.getPkOrg() != null) {
                sb.append(" AND T1.pk_org = ?  ");
                params.add(uecOrgDept.getPkOrg());
            }
            if (uecOrgDept.getPkDept() != null) {
                sb.append(" AND ( T1.pk_fatherorg = ? or T1.Pk_dept = ? ) ");
                params.add(uecOrgDept.getPkDept());
                params.add(uecOrgDept.getPkDept());
            }
            if (null != uecOrgDept.getName()) {
                sb.append(" AND T1.NAME like ? ");
                params.add("%" + StringEscapeUtils.escapeSql(uecOrgDept.getName()) + "%");
            }
            if (!"区本部".equals(orgName)) {
                sb.append(" AND T2.Name LIKE ? ");
                params.add("%" + StringEscapeUtils.escapeSql(orgName) + "%");
            }
            sb.append(" ORDER BY T1.code");
            return super.jdbcFindPageInfo(sb.toString(), params, currentPage, pageSize, UecOrgDept.class);
        }
        return null;
    }
}
